package pl.edu.pw.perg.compinfodb.xls.from.commandline;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import pl.edu.pw.perg.compinfodb.xls.from.InvalidMetadataFormatException;
import pl.edu.pw.perg.compinfodb.xls.from.XLSMetadataReader;

public class XLSCommandlineValidator extends XLSMetadataReader{

	private List<XLSSheets> states = new ArrayList<XLSSheets>();
	private static int counter = 0;
	private static final Logger logger = LoggerFactory.getLogger(XLSCommandlineValidator.class);
	
	
	public static void main(String[] args) {
	  
		XLSCommandlineValidator xls = new XLSCommandlineValidator();
		try{
			xls.getFields();
			xls.getTypes();
			xls.getElementsStructure();
			xls.getConnections();
			
			xls.getPhysicalElements();
			xls.getHistory();
			
			if (xls.states.size() != 0) {
				logger.error("Total number of errors: " + counter);
				logger.error("Errors in sheets: ");
              for (XLSSheets sheet : xls.states) {
            	  logger.error(sheet.getName());
              }
            }
			else
			  logger.info("All spreadsheets are valid.");
			
			
		}catch (ArrayIndexOutOfBoundsException e) {
			logger.error("Column index out of bounds: " 
              + e.getMessage() + ". Check properties file!\n", e);
          
          System.exit(1);
      }catch (Exception e) {
    	  logger.error("Exception occured. Please report.", e);
			System.exit(1);
		}
		
	}
	@Override
	protected void handleFormatError(InvalidMetadataFormatException e) {
	  counter++;
	  if(states == null)
	    states = new LinkedList<XLSSheets>();
	  if(!states.contains(e.getSheet()))
	    states.add(e.getSheet());
	  	logger.error(e.getMessage());
	}
}
